package hibu.LeiJonGun.customViews;

import hibu.LeiJonGun.activities.ActFindUser;
import hibu.LeiJonGun.activities.ActNetwork;
import hibu.LeiJonGun.seniorservice.R;
import hibu.LeiJonGun.seniorservice.sizeType;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * Written by: Christian L. Gunvaldsen
 * 
 * This classed is being used by 
 * ActFindUser, ActNetwork and represent a user
 */
public class CustomViewUser extends CustomAbstractViewGroup implements OnClickListener {
	private boolean isViewSelected = false;
	private TextView lblName;
	
// the constructors:
	public CustomViewUser(Context context) {
	    super(context);
	}
	
	public CustomViewUser(Context context, int viewIdx, String text, String username, sizeType screenSize) throws ClassCastException { // this one is being used
		super(context);
		if (!context.getClass().equals(ActFindUser.class) && !context.getClass().equals(ActNetwork.class)) 
			throw new ClassCastException("Wrong context: "+context.getClass()); // to make sure its the right context
		this.viewIdx=viewIdx;
		this.username=username;
		this.screenSize=screenSize;
		getScreenSize(); // check screen size and get sizes for the view + text
		initView(text);
	}
	
	private void initView(String text) { // custom for this textview
		//Set background:
		this.setBackgroundResource(R.drawable.lbl_find_user);
		//Listener:
		this.setOnClickListener(this); // is it own listener
		//Text:
		lblName = new TextView(this.getContext());
		lblName.setText(text);
		lblName.setTextColor(this.getResources().getColor(R.color.lblText));
		lblName.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
	    //set position, size and text size
  		RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,marginTopPx); // width and height of the view
  		rlp.setMargins(0, marginTopPx * viewIdx, 0, 0); // (marginTopPx * viewIdx) if marginTopPx is 100 and the viewIdx is 1, the view will have a marginTop of 100
  		this.setLayoutParams(rlp);
  		this.addView(lblName);
  		setPadding();
	}
	
	
	public void setIsViewSelected(boolean isViewSelected) { // is called from ActFindUser to unselect all view
		this.isViewSelected=isViewSelected;
		if (!isViewSelected) {
			this.setBackgroundResource(R.drawable.lbl_find_user); // ActViewAssignment uses a selector
			setPadding();
		}
	}
	
	public void onClick(View v) { // handles it own click
		if (isViewSelected) {
			isViewSelected = false;
			this.setBackgroundResource(R.drawable.lbl_find_user); // ActViewAssignment uses a selector
			
		} else {
			this.setBackgroundResource(R.drawable.lbl_find_user_selected); // ActViewAssignment uses a selector
			isViewSelected = true;
		}
		if(this.getContext().getClass().equals(ActFindUser.class)) ((ActFindUser)this.getContext()).userClicked(viewIdx,isViewSelected); //Update activity
		if(this.getContext().getClass().equals(ActNetwork.class)) ((ActNetwork)this.getContext()).userClicked(viewIdx,isViewSelected); //Update activity
	
		setPadding();
	}
	
	@Override
	protected void onLayout(boolean bool, int l, int t, int r, int b) { // position child views
		
		for (int i =0; i<this.getChildCount();i++) {
			View Child = this.getChildAt(i);
			if (Child.getClass().equals(TextView.class)) {t =padding;l = padding;} // padding to textbox
			 
			Child.layout(l, t, r, b);	
		}
		
	}
	
	public boolean getIsViewSelected() {
		return isViewSelected;
	}
}
